package cn.iocoder.yudao.module.wine.dal.mysql.stockinfoitem;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.wine.controller.admin.stockinfoitem.vo.StockInfoitemPageReqVO;
import cn.iocoder.yudao.module.wine.dal.dataobject.businessuser.BusinessUserDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.stockinfo.StockInfoDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.stockinfoitem.StockInfoitemDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.wine.controller.app.stockinfoitem.vo.*;

/**
 * 出入库信息项 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface StockInfoitemMapper extends BaseMapperX<StockInfoitemDO> {

    /**
     * 查找相同的条码信息
     * @param infoId
     * @param infoBn
     * @return
     */
    default StockInfoitemDO selectByBn(Long infoId, String infoBn) {
        return selectOne(new LambdaQueryWrapperX<StockInfoitemDO>()
                .eqIfPresent(StockInfoitemDO::getInfoId, infoId)
                .eqIfPresent(StockInfoitemDO::getInfoBn, infoBn)
                .last("limit 1")
        );
    }

    default PageResult<StockInfoitemDO> selectPage(StockInfoitemPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<StockInfoitemDO>()
                .eqIfPresent(StockInfoitemDO::getInfoId, reqVO.getInfoId())
                .eqIfPresent(StockInfoitemDO::getInfoSn, reqVO.getInfoSn())
                .eqIfPresent(StockInfoitemDO::getInfoNums, reqVO.getInfoNums())
                .eqIfPresent(StockInfoitemDO::getInfoBn, reqVO.getInfoBn())
                .betweenIfPresent(StockInfoitemDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(StockInfoitemDO::getId));
    }

    default List<StockInfoitemDO> getStockInfoitemList(AppStockInfoitemPageReqVO reqVO){
        return selectJoinList( StockInfoitemDO.class ,new MPJLambdaWrapperX<StockInfoitemDO>()
                .eqIfPresent(StockInfoitemDO::getInfoId, reqVO.getInfoId())
                .eqIfPresent(StockInfoitemDO::getInfoSn, reqVO.getInfoSn())
                .eqIfPresent(StockInfoitemDO::getInfoNums, reqVO.getInfoNums())
                .eqIfPresent(StockInfoitemDO::getInfoBn, reqVO.getInfoBn())
                .rightJoin(StockInfoDO.class,StockInfoDO::getId,StockInfoitemDO::getInfoId)
                .eqIfExists(StockInfoDO::getUserId, SecurityFrameworkUtils.getLoginUserId())
                .orderByDesc(StockInfoitemDO::getCreateTime));
    }
}
